package cn.tedu.csmall.passport.repository.impl;

import cn.tedu.csmall.commons.consts.JwtCacheConsts;
import cn.tedu.csmall.commons.pojo.po.AdminLoginInfoPo;
import cn.tedu.csmall.passport.repository.AdminJwtRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Repository
public class AdminJwtRepositoryImpl implements AdminJwtRepository, JwtCacheConsts {

    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void saveLoginInfo(String jwt, AdminLoginInfoPo adminLoginInfoPO, long jwtDuration) {
        String key = ADMIN_JWT_PREFIX + jwt;
        redisTemplate.opsForValue().set(key,adminLoginInfoPO,jwtDuration, TimeUnit.MILLISECONDS);
    }

    @Override
    public AdminLoginInfoPo getLoginInfo(String jwt) {
        String key = ADMIN_JWT_PREFIX + jwt;
        Serializable serializable = redisTemplate.opsForValue().get(key);
        AdminLoginInfoPo adminLoginInfoPo = (AdminLoginInfoPo) serializable;
        return adminLoginInfoPo;
    }

    @Override
    public Boolean delete(String jwt) {
        String key = ADMIN_JWT_PREFIX + jwt;
        return redisTemplate.delete(key);
    }
}
